﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DATA
{
    public class ManufacturerREP
    {
        MODELGENERALDataContext context = new MODELGENERALDataContext();


        public List<Manufacturer> GetAllManufacturers()
        {
            List<Manufacturer> acc = new List<Manufacturer>();

            var acounts = from a in context.Manufacturers
                          select a;
            foreach (var account in acounts)
            {
                acc.Add(account);
            }
            return acc;
        }

        public List<Manufacturer> GetActiveManufacturer()
        {
            List<Manufacturer> acc = new List<Manufacturer>();

            var acounts = from a in context.Manufacturers

                          select a;
            foreach (var account in acounts)
            {
                acc.Add(account);
            }
            return acc;
        }

        public bool CheckDuplicate(int AccountID)
        {
            Manufacturer newacc = new Manufacturer();

            for (int i = 0; i < (int)(context.Manufacturers.Count()); i++)
            {
                Manufacturer acc = context.Manufacturers.Single(c => c == newacc);
                if (AccountID.Equals(acc.manufacturerID))
                {
                    return true;
                }




            }
            return false;
        }

        public bool AddManufacturer(Manufacturer newAccount)
        {
            Manufacturer newacc = new Manufacturer();
            Manufacturer acc = context.Manufacturers.Single(c => c == newacc);
            if (CheckDuplicate(newAccount.manufacturerID) == false)
            {
                context.Manufacturers.InsertOnSubmit(acc);

                context.SubmitChanges();
            }

            return true;

        }
        public void deleteManufacturer(int AccountID)
        {
            var acc = from c in context.Manufacturers
                      where c.manufacturerID == AccountID
                      select c;

            context.Manufacturers.DeleteAllOnSubmit(acc);
            context.SubmitChanges();


        }

        public bool UpdateManufacturer(Manufacturer accountup)
        {
            try
            {
                Manufacturer acc = context.Manufacturers.Single(
                                                        nv => nv.manufacturerID == accountup.manufacturerID
                                                        );
                acc.name = accountup.name;
                acc.isActived = accountup.isActived;

                acc.description = accountup.description;
    



                context.SubmitChanges();
            }

            catch (System.Exception ex)
            {
                return false;
            }
            return true;
        }
    }
}
